import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.3
import com.RspInterface 1.0


//强平单处理
Item {
    anchors.fill: parent
    property bool have_cust: false //是否有客户号列
    property ListModel fOrderModel: ListModel {
    }

    //参数row为-1时代表全部
    signal checkRowChange(int row, bool bCheck)

    onFOrderModelChanged: checkRowChange(-2, false)
    //    ColumnLayout {
    //        anchors.fill: parent
    TableView {
        id: orderList
        anchors.left: parent.left
        anchors.leftMargin: 5
        anchors.right: parent.right
        anchors.rightMargin: 5
        anchors.top: parent.top
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 30
        model: fOrderModel
        //            Layout.fillHeight: true
        //                    property int cols: value
        TableViewColumn {
            role: "read_send"
            width: 20
            movable: false
            delegate: Component {
                Rectangle {
                    CheckBox {
                        id: ctrl
                        anchors.horizontalCenter: parent.horizontalCenter
                        checked: styleData.value
                    }

                    MouseArea {
                        //lqh 2018-8-1 这个MouseArea让全选按钮无法控制点击过的checkbox的bug消失了，神奇
                        //估计是屏蔽了CheckBox本身的响应，由外部控制是否check
                        anchors.fill: parent
                        onClicked: checkRowChange(styleData.row, !ctrl.checked)
                    }
                }
            }
        }

        TableViewColumn {
            title: "生成序号"
            role: "create_id"
            resizable: true
            width: 60
        }
        TableViewColumn {
            title: "合约代码"
            role: "prod_code"
            resizable: true
            width: 55
        }
        TableViewColumn {
            title: "买卖方向"
            role: "sale_type"
            resizable: true
            width: 55
        }
        TableViewColumn {
            title: "委托价格"
            role: "price"
            resizable: true
            width: 55
            horizontalAlignment: Text.AlignRight
        }
        TableViewColumn {
            title: "委托数量"
            role: "amount"
            resizable: true
            width: 55
            horizontalAlignment: Text.AlignRight
        }
        TableViewColumn {
            title: "剩余数量"
            role: "remain_amount"
            resizable: true
            width: 50
            horizontalAlignment: Text.AlignRight
        }
        TableViewColumn {
            title: "强平单号"
            role: "order_id"
            resizable: true
            width: 80
        }
        TableViewColumn {
            title: "本地报单号"
            role: "local_order_no"
            resizable: true
            width: 80
        }
        TableViewColumn {
            title: "系统报单号"
            role: "sys_no"
            resizable: true
            width: 80
        }

        TableViewColumn {
            title: "状态"
            role: "order_state"
            resizable: true
            width: 50
        }
        TableViewColumn {
            title: "信息"
            role: "info"
            resizable: true
            width: 80
        }

        Component.onCompleted: {
            if (have_cust) {
                var str = 'import QtQuick 2.0;import QtQuick.Controls 1.4;TableViewColumn {title: \"客户号\";role: \"AcctNo\";resizable: true;width: 90;}'
                orderList.insertColumn(1, Qt.createQmlObject(str, orderList,
                                                             "dynamicSnippet1"))
                console.log("insert cust column " + str)
            }
        }
    }
    RowLayout {
        anchors.left: parent.left
        anchors.leftMargin: 10
        anchors.right: parent.right
        anchors.rightMargin: 10
        anchors.bottom: parent.bottom
        CheckBox {
            text: "全选"
            onCheckedChanged: {
                for (var i = 0; i < fOrderModel.count; i++) {
                    fOrderModel.get(i).read_send = checked
                }

                checkRowChange(-1, checked)
            }
        }
        Item {
            Layout.fillWidth: true
        }

        Button {
            text: "全部清除"
            onClicked: fOrderModel.clear()
        }
        Button {
            text: "清除选中"
            onClicked: {
                for (var i = fOrderModel.count - 1; i >= 0; i--) {
                    if (fOrderModel.get(i).read_send)
                        fOrderModel.remove(i)
                }
            }
        }
        Button {
            text: "清除废单"
            onClicked: {
                for (var i = fOrderModel.count - 1; i >= 0; i--) {
                    var state = fOrderModel.get(i).order_state
                    if (state === "应答失败" || state === "用户删除" || state === "系统删除"
                            || state === "全部成交" || state === "错误委托")
                        fOrderModel.remove(i)
                }
            }
        }
        Button {
            text: "撤单"
            onClicked: {

                var tipMsg = ""
                for (var k = 0; k < fOrderModel.count; k++) {
                    if (!fOrderModel.get(k).read_send)
                        continue
                    var rsp = g_MainQuo.sendForceOrder(fOrderModel.get(
                                                           k).sys_no)
                    if ("" === rsp)
                        tipMsg += "强平单[" + fOrderModel.get(
                                    k).order_no + "]撤销成功!\n"
                    else
                        tipMsg += "强平单[" + fOrderModel.get(
                                    k).order_no + "]撤销失败!原因:" + rsp + "\n"
                }

                if (tipMsg.length > 0) {
                    tipInput.text = "强平单撤单结果："
                    tipInput.open()
                    tipInput.informativeText = tipMsg
                }
            }
        }
        Button {
            text: "发送"
            onClicked: {
                var iSended = -1
                for (var i = fOrderModel.count - 1; i >= 0; i--) {
                    if (fOrderModel.get(i).local_order_no !== undefined
                            && fOrderModel.get(i).local_order_no.length > 0) {
                        console.log("checkOrder " + fOrderModel.get(
                                        i).local_order_no)
                        iSended = i
                        break
                    }
                }

                if (iSended != -1) {
                    tipInput.text = "序号为[" + fOrderModel.get(
                                iSended).create_id + "]强平单已发送过,不能重复发送!"
                    tipInput.open()
                    return
                }

                //先刷新一次3063客户信息，作为强平场景 TODO
                var tipMsg = ""
                var batchNo = Qt.formatDateTime(new Date(),
                                                "MMddhhmmss") + createID(2)
                for (var k = 0; k < fOrderModel.count; k++) {
                    if (!fOrderModel.get(k).read_send)
                        continue

                    var orderInfo = "#lbatchno=" + batchNo
                    orderInfo += "#fc_order=" + fOrderModel.get(k).create_id
                    orderInfo += "#prod_code=" + fOrderModel.get(k).prod_code
                    var bsText = fOrderModel.get(k).sale_type
                    if (bsText.indexOf("买") >= 0)
                        orderInfo += "#buyorsell=b"
                    else
                        orderInfo += "#buyorsell=s"
                    orderInfo += "#price=" + fOrderModel.get(k).price
                    orderInfo += "#volume=" + fOrderModel.get(k).amount
                    var rsp = g_MainQuo.sendForceOrder(fOrderModel.get(
                                                           k).acct_no,
                                                       orderInfo)
                    var rspMsg = rsp.get("rsp_msg")
                    console.log("sendForceOrder " + rspMsg)
                    if (rspMsg === undefined) {
                        tipMsg += "序号为[" + fOrderModel.get(
                                    k).create_id + "]的报入成功\n"
                        fOrderModel.setProperty(k, "order_id",
                                                rsp.get("f_order_no"))
                        fOrderModel.setProperty(k, "local_order_no",
                                                rsp.get("local_order_no"))
                        fOrderModel.setProperty(k, "info", "已发送")
                    } else {
                        tipMsg += "序号为[" + fOrderModel.get(
                                    k).create_id + "]的报入失败!原因:" + rspMsg + "\n"
                        fOrderModel.get(k).info = rspMsg
                        fOrderModel.setProperty(k, "info", rspMsg)
                        fOrderModel.setProperty(k, "order_state",
                                                rsp.get("order_state"))
                    }
                }

                if (tipMsg.length > 0) {
                    tipInput.text = "强平单发送结果："
                    tipInput.open()
                    tipInput.informativeText = tipMsg
                }
            }
        }
        Item {
            Layout.preferredWidth: 10
        }
    }

    //    Component.onCompleted: {
    //        if (have_cust) {
    //            var str = 'import QtQuick 2.0;import QtQuick.Controls 1.4;TableViewColumn {title: \"客户号\";role: \"AcctNo\";resizable: true;width: 90;}'
    //            orderList.insertColumn(1, Qt.createQmlObject(str, orderList,
    //                                                         "dynamicSnippet1"))
    //            console.log("insert cust column " + str)
    //        }
    //    }
    //    onCheckRowChange: {
    //        if (row >= 0) {
    //            fOrderModel.get(row).read_send = bCheck
    //        }
    //    }

    //    ListModel {
    //        id: fOrderModel
    //    }
    //    }
}
